package com.cpe.web.webinitializer;

import javax.servlet.Filter;

import org.springframework.core.annotation.Order;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.cpe.web.config.ApplicationConfig;
import com.cpe.web.config.WebApplicationConfig;

import jodd.util.StringPool;

/**
 * 1.该类实现了{@code WebApplicationInitializer}，该接口的所有实现类会在Servlet容器启动时自动被加载运行 <br>
 * 2.Servlet3.0+新特性：web.xml零配置，都硬编码到{@code WebApplicationInitializer}的实现类中<br>
 * 
 * @Description TODO Servlet容器初始化配置
 * @author zhoud
 * @date 2015年10月28日 下午9:13:08
 * @since Servlet3.0
 */
@Order(2)
public class SpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/**
	 * 应用上下文（与web无关）
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] { ApplicationConfig.class };
	}

	/**
	 * web上下文
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] { WebApplicationConfig.class };
	}

	/**
	 * DispatcherServlet的映射路径
	 */
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/*" };
	}

	/**
	 * 1.注册过滤器，映射路径与DispatcherServlet一致<br>
	 * 2.路径不一致的过滤器需要注册到另外的WebApplicationInitializer中<br>
	 */
	@Override
	protected Filter[] getServletFilters() {
		// 字符过滤器
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding(StringPool.UTF_8);
		characterEncodingFilter.setForceEncoding(true);
		return new Filter[] { characterEncodingFilter };
	}
}
